'use strict';
application.controller('applicationCtrl', ['$scope', '$sce', '$mdUtil',
  function (scope, sce, mdUtil) {
    let mainContentArea = document.querySelector("body");
    let scrollContentEl = mainContentArea.querySelector('md-content[md-scroll-y]');
    scope.scrollTop = function() {
      mdUtil.animateScrollTo(scrollContentEl, 0, 200);
    }
  }
]).directive('scrollClass', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attr) {
      let scrollMainChildren = element.parent().children();
      let isScrolling = false;
      // Initial update of the state.
      updateState();
      // Register a scroll listener, which updates the state.
      scrollMainChildren.on('scroll', updateState);
      function updateState() {
        // md-content element
        let newState = scrollMainChildren[1].scrollTop !== 0;
        if (newState !== isScrolling) {
          element.toggleClass(attr.scrollClass, newState);
        }
        isScrolling = newState;
      }
    }
  };
});
